﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsVoteItem
    {
        private readonly KeRui.DAL.CmsVoteItem dal = new KeRui.DAL.CmsVoteItem();

        public int Add(KeRui.Model.CmsVoteItem model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsVoteItem> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsVoteItem> list = new List<KeRui.Model.CmsVoteItem>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsVoteItem item = new KeRui.Model.CmsVoteItem();
                    if (dt.Rows[i]["VoteItemID"].ToString() != "")
                    {
                        item.VoteItemID = int.Parse(dt.Rows[i]["VoteItemID"].ToString());
                    }
                    if (dt.Rows[i]["VoteID"].ToString() != "")
                    {
                        item.VoteID = int.Parse(dt.Rows[i]["VoteID"].ToString());
                    }
                    item.Title = dt.Rows[i]["Title"].ToString();
                    if (dt.Rows[i]["VoteNum"].ToString() != "")
                    {
                        item.VoteNum = int.Parse(dt.Rows[i]["VoteNum"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int VoteItemID)
        {
            this.dal.Delete(VoteItemID);
        }

        public bool Exists(int VoteItemID)
        {
            return this.dal.Exists(VoteItemID);
        }

        public int GetCount(string Conditions)
        {
            string strWhere = "";
            if (Conditions != "")
            {
                strWhere = " VoteID=" + int.Parse(Conditions);
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsVoteItem", strWhere);
        }

        public List<KeRui.Model.CmsVoteItem> GetList(string VoteID)
        {
            string strWhere = " VoteID=" + VoteID;
            string strOrder = " VoteItemID ";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsVoteItem", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsVoteItem> GetListByCache(string strCondition)
        {
            string cacheKey = "CmsVoteItemList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsVoteItem>) cache;
        }

        public KeRui.Model.CmsVoteItem GetModel(int VoteItemID)
        {
            return this.dal.GetModel(VoteItemID);
        }

        public KeRui.Model.CmsVoteItem GetModelByCache(int VoteItemID)
        {
            string cacheKey = "CmsVoteItemModel-" + VoteItemID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(VoteItemID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsVoteItem) cache;
        }

        public int GetSum(int VoteID)
        {
            return this.dal.GetItemCount(VoteID);
        }

        public void Update(KeRui.Model.CmsVoteItem model)
        {
            this.dal.Update(model);
        }

        public int UpdateSum(string VoteItemID, KeRui.Model.CmsVoteIp model)
        {
            return this.dal.UpdateNum(VoteItemID, model);
        }
    }
}

